<?php

namespace App\Models\Purchase;


use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;

class PurchaseSettleOrder extends Model
{
    protected $table = 'purchase_settle_order';
    public $timestamps = false;
    protected $primaryKey = 'id';
    protected $guarded = ['_token'];
    

    /**
     *获取列表 
     */
    public static function getList(Request $request, $cpid, $manger_id = 0, $isgetall = 0)
    {
        $query = self::where(['cpid' => $cpid, 'is_del' => 1])->orderBy('create_at', 'desc');
        if ($manger_id > 0) {
            $query->where('manger_id', $manger_id);
        }



        $pa_type = $request->pa_type;
        if (in_array($pa_type, [1, 2, 3,4])) {
            $query->where('pa_type', $pa_type);
        }


        $pa_settle_company_name = $request->pa_settle_company_name; //结算公司名称
        if ($pa_settle_company_name != '') {
            $query->where('pa_settle_company_name', 'like', '%' . $pa_settle_company_name . '%');
        }

        $archive_spec = $request->archive_spec; //规格
        if ($archive_spec != '') {
            $query->where('archive_spec', 'like', '%' . $archive_spec . '%');
        }

        $company_name = $request->company_name; //供应商名称
        if ($company_name != '') {
            $query->where('company_name', 'like', '%' . $company_name . '%');
        }

        $sales_manger_name = $request->sales_manger_name; //业务员姓名
        if ($sales_manger_name != '') {
            $query->where('sales_manger_name', 'like', '%' . $sales_manger_name . '%');
        }

        $carship_no = $request->carship_no; //车船号
        if ($carship_no != '') {
            $query->where('carship_no', 'like', '%' . $carship_no . '%');
        }

        $po_order_sn_code = $request->po_order_sn_code; //采购订单单据编号
        if ($po_order_sn_code != '') {
            $query->where('po_order_sn_code', 'like', '%' . $po_order_sn_code . '%');
        }

        $order_sn_code = $request->order_sn_code; //单据编号
        if ($order_sn_code != '') {
            $query->where('order_sn_code', 'like', '%' . $order_sn_code . '%');
        }

        

        if ($isgetall == 0) {
            $limit = $request->limit ?? 10;
            $list = $query->paginate($limit)->toArray();
            return paginateDataToArray($list);
        } else {
            $list = $query->get()->toArray();
            return ['list' => $list];
        }
    }
}
